package main

/*

#cgo LDFLAGS: -L../../deps -lnetdata_linux_x86_64_gnu -lm -Wl,-rpath=../../deps

*/
import "C"

import (
	"fmt"
	"nd-app/pkg/app"
	"nd-app/pkg/logger"
	"os"

	"github.com/urfave/cli/v2"
)

func main() {
	cliApp := cli.NewApp()
	cliApp.Name = "nd-app"
	cliApp.Usage = "./nd-app --cfg [configuration file]"
	cliApp.Action = action
	cliApp.Flags = getCliFlags()

	logger.AppLog.Infoln("App Name:", cliApp.Name)

	fmt.Println("args:", os.Args)

	if err := cliApp.Run(os.Args); err != nil {
		logger.AppLog.Errorln("Failed to run nd-app:", err)
		return
	}
}

func action(c *cli.Context) error {
	cfg := c.String("cfg")

	err := app.Init(cfg)
	if err != nil {
		logger.AppLog.Errorln("app init fail:", err)
		return err
	}
	err = app.Start()
	if err != nil {
		logger.AppLog.Errorln("app test fail:", err)
		return err
	}
	err = app.Wait()
	if err != nil {
		logger.AppLog.Errorln("app wait fail:", err)
		return err
	}

	app.Final()

	return nil
}

func getCliFlags() []cli.Flag {
	return []cli.Flag{
		&cli.StringFlag{
			Name:     "cfg",
			Value:    "",
			Usage:    "--cfg [configuration file]",
			Required: false,
		},
	}
}
